#ifndef __PROCESS_POOL_HPP__
#define __PROCESS_POOL_HPP_
#include "Task.hpp"
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string>

// 管道对象
class Channel
{
public:
    Channel(int wfd, pid_t id)
        : _wfd(wfd), _child_id(id)
    {
        _name = "Channel-" + std::to_string(wfd) + "-" + std::to_string(id);
    }

    void Wait()
    {
        pid_t rid = waitpid(_child_id, nullptr, 0);
        (void)rid;
    }

    void Close()
    {
        close(_wfd);
    }

    void Send(int code)
    {
        int n = write(_wfd, &code, sizeof(code));
        (void)n;
    }

    int GetDd() { return _wfd; }
    pid_t GetId() { return _child_id; }
    std::string GetName() { return _name; }

    ~Channel()
    {
    }

private:
    int _wfd;
    pid_t _child_id;
    std::string _name;
};

// 管道管理
class ChannelMange
{
public:
    ChannelMange()
        : _next(0)
    {
    }

    void Insert(int wfd, pid_t child_id)
    {
        _cl.emplace_back(wfd, child_id);
    }

    void PrintName()
    {
        for (auto &cl : _cl)
        {
            std::cout << cl.GetName() << std::endl;
        }
    }

    Channel &Select()
    {
        auto &c = _cl[_next];
        ++_next;
        _next %= _cl.size();
        return c;
    }

    void CloseWrite()
    {
        for (auto &cl : _cl)
        {
            cl.Close();
            std::cout << "关闭写端-" << cl.GetName() << std::endl;
        }
    }

    void CloseChild()
    {
        for (auto &cl : _cl)
        {
            cl.Wait();
            std::cout << "回收进程-" << cl.GetName() << std::endl;
        }
    }

    ~ChannelMange() {}

private:
    std::vector<Channel> _cl;
    int _next;
};

class ProcessPoll
{
public:
    ProcessPoll(int num)
        : _process_num(num)
    {
        _tm.Register(PrintLog);
        _tm.Register(DownLoad);
        _tm.Register(UpLoad);
    }

    void Debug()
    {
        _cm.PrintName();
    }

    void Work(int rfd)
    {
        while(true)
        {
            int code = 0;
            ssize_t n = read(rfd, &code, sizeof(code));
            if(n > 0)
            {
                if(n != sizeof(code))
                {
                    continue;
                }
                std::cout << "子进程[" << getpid() << "->收到一个任务]" << std::endl;
                _tm.Execute(code);
            }
            else if(n == 0)
            {
                std::cout << "子进程退出" << std::endl;
                break;
            }
            else{
                std::cout << "错误!" << std::endl;
                break;
            }
        }
    }

    bool Start()
    {
        for (int i = 0; i < _process_num; ++i)
        {
            int pipefd[2] = {0};
            int n = pipe(pipefd);
            if (n < 0)
                return false;

            // 创建子进程
            pid_t id = fork();
            if (id < 0)
                return false;
            else if (id == 0)
            {
                // child
                // close write
                close(pipefd[1]);
                Work(pipefd[0]);
                close(pipefd[0]);
                exit(0);
            }
            else
            {
                // father
                // 关闭读端
                close(pipefd[0]);
                _cm.Insert(pipefd[1], id);
            }
        }
        return true;
    }

    void Stop()
    {
        _cm.CloseWrite();
        _cm.CloseChild();
    }

    void Run()
    {
        // 选择一个任务
        int taskcode = _tm.Code();

        // 选择一个信道
        auto& c = _cm.Select();
        std::cout << "选择了一个任务" << c.GetName() << std::endl;
        
        // 发送任务
        c.Send(taskcode);
        std::cout << "发送了一个任务码" << taskcode << std::endl;
    }

private:
    ChannelMange _cm;
    TaskManager _tm;
    int _process_num; // 创建子进程的个数
};

#endif